xenpm: add timeout option to 'xenpm start' command.
authorKeir Fraser <keir.fraser@citrix.com>
Thu, 19 Mar 2009 10:09:59 +0000 (10:09 +0000)
committerKeir Fraser <keir.fraser@citrix.com>
Thu, 19 Mar 2009 10:09:59 +0000 (10:09 +0000)
Thus we can sample a fixed time of period without manual interruption.

Signed-off-by: Guanqun Lu <guanqun.lu@intel.com>
tools/misc/xenpm.c

index 08e26695f92f9b4160444b7c2a1dc001e46a7f82..6dd4c691fe667149cce88240f39d036c9ba16d87 100644 (file)
@@ -59,8 +59,8 @@ void show_help(void)
             " set-up-threshold      [cpuid] <num> set up threshold on CPU <cpuid> or all\n"
             "                                     it is used in ondemand governor.\n"
             " get-cpu-topology                    get thread/core/socket topology info\n"
-            " start                               start collect Cx/Px statistics,\n"
-            "                                     output after CTRL-C or SIGINT.\n"
+            " start [seconds]                     start collect Cx/Px statistics,\n"
+            "                                     output after CTRL-C or SIGINT or several seconds.\n"
             );
 }
 /* wrapper function */
@@ -353,6 +353,16 @@ void start_gather_func(int argc, char *argv[])
 {
     int i;
     struct timeval tv;
+    int timeout = 0;
+
+    if ( argc == 1 )
+    {
+        sscanf(argv[0], "%d", &timeout);
+        if ( timeout <= 0 )
+            fprintf(stderr, "failed to set timeout seconds, falling back...\n");
+        else
+            printf("Timeout set to %d seconds\n", timeout);
+    }
 
     if ( gettimeofday(&tv, NULL) == -1 )
     {
@@ -408,7 +418,21 @@ void start_gather_func(int argc, char *argv[])
         free(cxstat);
         return ;
     }
-    printf("Start sampling, waiting for CTRL-C or SIGINT signal ...\n");
+
+    if ( timeout > 0 )
+    {
+        if ( signal(SIGALRM, signal_int_handler) == SIG_ERR )
+        {
+            fprintf(stderr, "failed to set signal alarm handler\n");
+            free(sum);
+            free(pxstat);
+            free(cxstat);
+            return ;
+        }
+        alarm(timeout);
+    }
+
+    printf("Start sampling, waiting for CTRL-C or SIGINT or SIGALARM signal ...\n");
 
     pause();
 }